
/*sessionStorage.setItem("userId","13485221331063032"); 
sessionStorage.setItem("name","苏慧");
sessionStorage.setItem("jobnumber","0");
sessionStorage.setItem("deptId","55088496");
*/

//sessionStorage.setItem("userId","014136441221947361"); 
//sessionStorage.setItem("name","史鹏飞");
//sessionStorage.setItem("jobnumber","2851,1086,1076,2861");
//sessionStorage.setItem("deptId","55088496");


//sessionStorage.setItem("userId","1304442705697854");
var thesum=0;    //待办事项返回数量
var resdata=[];  //待办事项返回数据
var sendData = {};  //待办事项
var pandianid;
var nowDate = getDay(0);
//sendData.planuserid =sessionStorage.getItem("userId");
sendData.flag=10;
sendData.flag1=12;
var daibannum = 0;
var corp = GetQueryString('corp')==null?"":GetQueryString('corp');
if(corp==''){
	corp = sessionStorage.getItem("corp");
}else{
	sessionStorage.setItem("corp",corp);
}


if(sessionStorage.getItem("name")){
	//alert(localStorage.getItem("name"));
//	setthenum();

	onload();
}else{

	var _config ;
	$.ajax({
		type : "post",
		data:JSON.stringify({"pageUrl":location.href.replace(/\\&/g,'%26'),"corp":corp}),
		contentType: "application/json; charset=utf-8",
		async: false,
		url : "../../dingding/getconfig",
		dataType : "json",
		success : function(suc) {
			_config = suc.config;
		}
	}); 
//	dd.config({
//		agentId : _config.agentid,
//		corpId : _config.corpId,
//		timeStamp : _config.timeStamp,
//		nonceStr : _config.nonceStr,
//		signature : _config.signature,
//		jsApiList : [ 'runtime.info', 'biz.contact.choose',
//				'device.notification.confirm', 'device.notification.alert',
//				'device.notification.prompt', 'biz.ding.post',
//				'biz.util.openLink' ]
//	});
	dd.ready(function() {
		//channel runtime  requestOperateAuthCode requestAuthCode
	    dd.runtime.permission.requestAuthCode({
	        corpId: _config.corpId,
	        onSuccess: function(result) {
	        	getuserId(result);
	        },
	        onFail : function(err) {
	        	alert("错误"+JSON.stringify(err))
	        }
	 
	    });
	});
	function getuserId(result){
			    //$("body").text('https://oapi.dingtalk.com/user/getuserinfo?code=' + result.code + '&access_token='+ _config.token);
		$.ajax({
			url : '../../dingding/ddEasy',
			type : 'POST',
			data:JSON.stringify({"code":result.code,"token":_config.token}),
			contentType: "application/json; charset=utf-8",
			dataType : "json",
			success : function(suc) {
			    if(suc.status == '1002'){
			    	sessionStorage.setItem("userId",suc.userJson.userid);
			    	sessionStorage.setItem("name",suc.userJson.name);
			    	sessionStorage.setItem("jobnumber",suc.userJson.jobnumber);
			    	sessionStorage.setItem("deptId",suc.userJson.department);
			    	sessionStorage.setItem("mobile",suc.userJson.mobile);
//					setthenum();
			    	onload();
			    }else{
			    	layer.alert(suc.msg);
			    }

			},
			error : function(xhr, errorType, error) {
					alert(errorType + ', ' + error);
				}
		});
	}
	dd.error(function(err) {
		alert('dd error: ' + JSON.stringify(err));
	});
}
//查询门店信息
function getShopInfo(){
	if($('#pandianShop').val() == ''){
		layer.alert('门店号不能为空')
	}else{
		var sendData = {};
		sendData.shopcode=sessionStorage.getItem("jobnumber");
		$.ajax({
			url: "getShopInfo"
			,type:'post'
			,async: false
			,data:JSON.stringify(sendData)
			,contentType: "application/json; charset=utf-8"
			,dataType:'json'
			,success: function(res){
				console.log(res)
				if(res.success){
					var resdata = res.data;
					$('#shopSelect').empty();
					for (var i = 0; i < resdata.length; i++) {
						$('#shopSelect').append('<option value="'+resdata[i].shopcode+'">'+resdata[i].shopname+'</option>');
					}
					if(resdata.length > 1){
						$('#shopList').show();
					}
					$('#shopInfo').text($('#shopSelect option:selected').text()+'('+$('#shopSelect').val()+')');
					
				}else{
					layer.alert(res.message);
				}
			}
			,error:function(XMLHttpRequest, textStatus, errorThrown) {
				 console.log(XMLHttpRequest);
				 console.log(textStatus);
				 console.log(errorThrown);
				 hideloading();
			}
		});
	}
	
}

function onload(){

	$('#name').text(sessionStorage.getItem("name"));
	getShopInfo();
	$('#shopSelect').on('change',function(){
		$('#shopInfo').text($('#shopSelect option:selected').text()+'('+$('#shopSelect').val()+')');
		daibannum = 0;
		getPandianJiancha();
		getPandianJianchaPingjia();
	});
	addClick();
	getPandianJiancha();
	getPandianJianchaPingjia();
}
function addClick(){
	$('.bb_img').on('click',function(){
		sessionStorage.setItem("shopcode",$('#shopSelect').val());
    	sessionStorage.setItem("shopname",$('#shopSelect option:selected').text());
    	sessionStorage.setItem("job","Store");
		window.location.href=$(this).attr('pageUrl');
	});
}
//查询门店盘点信息
function getPandianJiancha(){
	var sendData = {};
	sendData.shopcode=$('#shopSelect').val();
	sendData.pandianDate=nowDate;
//	sendData.flag=3;
	$.ajax({
		url: "../pandianevery/getPandianJiancha"
		,type:'post'
		,async: false
		,data:JSON.stringify(sendData)
		,contentType: "application/json; charset=utf-8"
		,dataType:'json'
		,success: function(res){
			console.log(res)
			
			if(res.success){
				var resdata = res.data;
				if(pandianid == undefined || pandianid == ''){
					pandianid = resdata[0].id;
				}
				if(resdata[0].flag == 3){
					getPandianJianShopDaibanList();
				}
			}else{
//				layer.alert(res.message);
				getPandianJianShopDaibanList();
			}
		}
		,error:function(XMLHttpRequest, textStatus, errorThrown) {
			 console.log(XMLHttpRequest);
			 console.log(textStatus);
			 console.log(errorThrown);
			 hideloading();
		}
	});
	
}
//获取门店待办信息
function getPandianJianShopDaibanList(){
	showloading();
	var sendData = {};
	sendData.flag = 0;
	if(pandianid != undefined && pandianid != ''){
		sendData.pandianid = pandianid;
	}else{
		sendData.pandianDate = getDay(1);
		sendData.shopcode = $('#shopSelect').val();
	}
	$.ajax({
		url: "../pandianevery/getPandianJianShopDaibanList"
		,type:'post'
		,dataType:'json'
//		,async: false
		,contentType: "application/json; charset=utf-8"
		,data:JSON.stringify(sendData) 
		,success: function(res){
			console.log(res)
			try {
				if(res.success){
					var data = res.data;
					if(data != undefined && data.length > 0){
						daibannum += data.length;
					}
					$('#daibannum').text(daibannum);
				}else{
//					layer.msg(res.message);
				}
			} catch (e) {
				console.log(e)
			} finally{
				hideloading();
			}
		}
    });
}
//评价信息列表
function getPandianJianchaPingjia(){
	showloading();
	var sendData = {};
	sendData.pingjiaFlag = 0;
	sendData.flag = 7;
	sendData.shopcode = $('#shopSelect').val();
	sendData.endDate = nowDate;
	sendData.startDate = nowDate;
	$.ajax({
		url: "../pandianevery/getPandianJiancha"
		,type:'post'
		,dataType:'json'
//		,async: false
		,contentType: "application/json; charset=utf-8"
		,data:JSON.stringify(sendData) 
		,success: function(res){
			console.log(res)
			try {
				if(res.success){
					var resdata = res.data;
					if(resdata != undefined && resdata.length > 0){
						daibannum += resdata.length;
					}
					$('#daibannum').text(daibannum);
					
				}else{
//					layer.alert(res.message);
				}
			} catch (e) {
				console.log(e)
			} finally{
				hideloading();
			}
		}
    });
}
